package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.gears.TopicsSheetListActivity;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.util.MobileUtils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class TopicSheetListAdapter extends BaseAdapter {
	private TopicsSheetListActivity activity;
	private ArrayList<CatalogItemBean> catalog_item_list = null;
	private BitmapUtils  bitmapUtil;
	public TopicSheetListAdapter(Activity activity, BitmapUtils bitmapUtil) {
		this.activity = (TopicsSheetListActivity) activity;
		this.bitmapUtil = bitmapUtil;
	}

	public void setData(ArrayList<CatalogItemBean> datas) {
		if (catalog_item_list == null) {
			catalog_item_list = new ArrayList<CatalogItemBean>();
		}
		if(null != datas){
			catalog_item_list.addAll(datas);
		}
	}

	@Override
	public int getCount() {
		return catalog_item_list != null && catalog_item_list.size() > 0?catalog_item_list.size():0;
	}

	@Override
	public Object getItem(int position) {
		return catalog_item_list != null && catalog_item_list.size() > 0?catalog_item_list.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHold hold;
		if (convertView == null) {
			hold = new ViewHold();
			convertView = LayoutInflater.from(activity).inflate(
					R.layout.layout_topics_sheet_item, null);
//			hold.rl_topics_sheet_icon = (RelativeLayout) convertView.findViewById(R.id.rl_topics_sheet_icon);
			//用户头像
			hold.user_head_icon = (ImageView) convertView.findViewById(R.id.user_head_icon);
			//用户昵称
			hold.tv_user_display_name = (TextView) convertView.findViewById(R.id.tv_user_display_name);
			//等级
			hold.tv_grade = (TextView) convertView.findViewById(R.id.tv_grade);
			//专题内容
			hold.tv_topic_sheet_title = (TextView) convertView.findViewById(R.id.tv_topic_sheet_title);
			//专题内容中的图片
			hold.iv_topic_content_image = (ImageView) convertView.findViewById(R.id.iv_topic_content_image);
			//动态设置布局的位置状态
			hold.rl_baby_setting = (RelativeLayout) convertView.findViewById(R.id.rl_baby_setting);
			//宝宝年龄
			hold.tv_baby_age = (TextView) convertView.findViewById(R.id.tv_baby_age);
			//发布时间
			hold.tv_post_topic_time = (TextView) convertView.findViewById(R.id.tv_post_topic_time);
			//回复数
			hold.tv_reply_count = (TextView) convertView.findViewById(R.id.tv_reply_count);
			
			convertView.setTag(hold);
			
		} else {
			hold = (ViewHold) convertView.getTag();
		}
		
		if (catalog_item_list == null)return null;
		final CatalogItemBean item = catalog_item_list.get(position);
		if (item != null && (item instanceof CatalogTopicItem)) {
			CatalogTopicItem topic = (CatalogTopicItem) item;
			if (topic.getItem() != null) {
				bindData(hold, topic.getItem());// 绑定数据
			}
		}
		
		return convertView;
	}

	private void bindData(ViewHold hold, TopicBean topic) {
		//用户头像
		if(topic.getPost_actor().getAvatar_file_meta()!=null){
			hold.user_head_icon.setImageDrawable(null);
			this.bitmapUtil.configDefaultLoadingImage(R.drawable.defult_user_icon);
			this.bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_user_icon);
			this.bitmapUtil.display(hold.user_head_icon, topic.getPost_actor().getAvatar_file_meta().getDownload_urls().getBig().getUrl());
		}else{
			hold.user_head_icon.setImageDrawable(null);
			hold.user_head_icon.setImageResource(R.drawable.defult_user_icon);
		}
		String level = "0";
		//等级
		if(topic.getPost_actor().getLevel() !=null){
			if(topic.getPost_actor().getLevel().length()>= 3){
				level = "99+";
			}else{
				level = topic.getPost_actor().getLevel()+"";
			}
		}
		hold.tv_grade.setText(level);
		//昵称
		hold.tv_user_display_name.setText(topic.getPost_actor().getDisplay_name() + "");
		//专题标题
		String title ="";
//		if(topic.getTitle() != null && topic.getTitle().length() > 10){
//			title = topic.getTitle().substring(0, 10);
//		}else{
			title = topic.getTitle();
//		}
		hold.tv_topic_sheet_title.setText(title);
		//内容图片
		if(topic.getFile_metas() !=null){
			hold.iv_topic_content_image.setVisibility(View.VISIBLE);
			hold.iv_topic_content_image.setImageDrawable(null);
			this.bitmapUtil.configDefaultLoadingImage(R.drawable.iask_main_middle_img);
			this.bitmapUtil.configDefaultLoadFailedImage(R.drawable.iask_main_middle_img);
			this.bitmapUtil.display(hold.iv_topic_content_image, topic.getFile_metas().get(0).getDownload_urls().getBig().getUrl());
			//dynamicSettingView(hold, MobileUtils.dpToPx(12));
		}else{
			hold.iv_topic_content_image.setImageDrawable(null);
			hold.iv_topic_content_image.setVisibility(View.GONE);
			//dynamicSettingView(hold, MobileUtils.dpToPx(35));
		}
		if(topic.getExtra_data() != null && topic.getExtra_data().getBaby_days() !=null){
			hold.tv_baby_age.setText(DateUtil.getDayTip(topic.getExtra_data().getBaby_days()));
		}else{
			hold.tv_baby_age.setText("1天");
		}
		//宝宝年龄
		
		//发布时间
		if(topic.getPost_time()!= null){
			hold.tv_post_topic_time.setText(DateUtil.gettopicDate(topic.getPost_time()) + "");
		}else{
			hold.tv_post_topic_time.setText(DateUtil.gettopicDate(System.currentTimeMillis()+""));
		}
		
		//回复数
		hold.tv_reply_count.setText(topic.getReply_count() + "");
		
	}
	
	
	private void dynamicSettingView(ViewHold hold, int marginTop){
		LayoutParams  params = (LayoutParams) hold.rl_baby_setting.getLayoutParams();
		params.topMargin = marginTop;
		hold.rl_baby_setting.setLayoutParams(params);
	}
	
	
	class ViewHold {
		RelativeLayout rl_topics_sheet_icon;
		RelativeLayout rl_content_baby;
		ImageView user_head_icon;
		TextView tv_user_display_name;
		TextView tv_grade;
		TextView tv_topic_sheet_title;
		ImageView iv_topic_content_image;
		RelativeLayout  rl_baby_setting;
		TextView tv_baby_age;
		TextView tv_post_topic_time;
		TextView tv_reply_count;
		
	}

}
